package com.basic.business.memoType;

import com.basic.business.common.constants.Constants;
import com.basic.business.common.redis.RedisCacheUtils;
import com.basic.business.common.utils.StringUtils;
import com.basic.business.memoType.domain.TOrdersMemoType;
import com.basic.business.order.domain.TOrdersMerge;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 订单备注类型工具类
 */
public class MemoTypeUtils {

    /**
     * 获取订单备注类型缓存
     */
    private static List<TOrdersMemoType> getMemoTypeCache() {
        return RedisCacheUtils.getCacheObject(Constants.MEMO_TYPE_REDIS_KEY);
    }

    /**
     * 判断订单备注是属于哪种类型
     */
    public static String getMemoType(List<TOrdersMerge> merges) {
        List<TOrdersMemoType> memoTypeCache = getMemoTypeCache();
        if (StringUtils.isNull(memoTypeCache)) {
            return StringUtils.EMPTY;
        }

        Map<String, String> memoLabelToValue = memoTypeCache.stream()
                .collect(Collectors.toMap(TOrdersMemoType::getMemoLabel, TOrdersMemoType::getMemoValue));

        return merges.stream()
                .filter(merge -> StringUtils.equals("WAIT_AUDIT",  merge.getSysStatus()))
                .map(TOrdersMerge::getSellerMemo)
                .filter(StringUtils::isNotEmpty)
                .flatMap(sellerMemo -> memoLabelToValue.entrySet().stream()
                        .filter(entry -> sellerMemo.contains(entry.getKey()))
                        .map(Map.Entry::getValue))
                .distinct() // 去重（可选）
                .collect(Collectors.joining(","));
    }

}
